<?php

/* 所有前台要用到的控制器
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class FrontAction {

    private $article;
    public $tpl;

    function __construct() {
        session_start();
        $this->tpl = new MyTpl;
        $this->tpl->assign('title', "phplin.net的博客");
        $web_title = $this->sel_set('curwebtitle');
        $web_no = $this->sel_set('curwebno');
        //设置最新文章的标题 及相关链接
        $this->sel_newtitle();
        $this->show_cate(); //显示文章的分类统计
        $this->tpl->assign('blog_title', $web_title);
        $this->tpl->assign('blog_no', $web_no);

        $this->tpl->assign('login_url', 'admin/login.php');
        $this->isLogin();
    }

    function index($m='', $cid='') {
        //默认的前台页面的显示
        //获取文章中的信息

        if ($m == 'cate') {
            $sql = 'SELECT `id` FROM `articles` WHERE `c_id`=\'' . $cid . '\'';
            $result = mysql_query($sql);
            $rows = mysql_num_rows($result);

            $page = new Page($rows, 4);
            $sql = 'SELECT `id`,`c_id`,`u_id`,`title`,`keywords`,`description`,`content`,`pub_date`,`show_times`,`cmt_count` FROM `articles` WHERE `c_id`=\'' . $cid . '\' ORDER BY `id` DESC ' . $page->limit . '';
            $result = mysql_query($sql);
        } else {
            $sql = 'SELECT `id` FROM `articles` ';
            $result = mysql_query($sql);
            $rows = mysql_num_rows($result);

            $page = new Page($rows, 4);
            $sql = 'SELECT `id`,`c_id`,`u_id`,`title`,`keywords`,`description`,`content`,`pub_date`,`show_times`,`cmt_count` FROM `articles` ORDER BY `id` DESC ' . $page->limit . '';
            $result = mysql_query($sql);
        }

        $data = array();
        while ($rows = mysql_fetch_assoc($result)) {
            $cate = $this->sel('title', 'categorys', $rows['c_id']);
            $user = $this->sel('login_name', 'users', $rows['u_id']);
            if (strlen($rows['content']) > 80) {
                $content = mb_substr($rows['content'], 0, 80);
                $rows['content'] = $content.'......';
            }

            $rows['c_id'] = $cate;
            $rows['u_id'] = $user;
            $data[] = $rows;
        }

        $this->tpl->assign('data', $data);
        $this->tpl->assign('fpage', $page->fpage());
        $this->tpl->display('index.tpl');
    }

    function run($act, $aid, $m, $cid) {
        session_start();

        if (empty($act) && empty($aid)) {

            $this->index($m, $cid);
        } elseif ($act == 'login') {
            //用户登录
            if ($_POST['submit']) {
                $name = empty($_POST['username']) ? '' : $_POST['username'];
                $pass = empty($_POST['password']) ? '' : $_POST['password'];
                if (!empty($name) && !empty($pass)) {
                    $login = $this->user_login($name, $pass);
                    if (empty($login)) {
                        // 没有登录成功 提示 并要求重新登录
                        show_msg('用户名或密码错误，请重新登录', '../../index.php');
                    } else {

                        show_msg('登录成功', '../../index.php?sid=' . session_id());
                    }
                } else {
                    show_msg('用户名、密码不能为空，请重新登录', '../../index.php');
                }
            }
        } elseif ($act == 'logout') {//用户退出
            $this->user_logout();
        } elseif (isset($aid)) {
            //显示文章页面
            $this->act($aid);
            //} elseif ($act == 'comment') {
            if (isset($_POST['submit'])) {
                $cmt = empty($_POST['FCKeditor1']) ? '' : $_POST['FCKeditor1'];
                if (empty($cmt)) {
                    $tishi = '<script>';
                    $tishi.='alert(\'请先输入评论之后再提交！\')';
                    $tishi.='</script>';
                    echo $tishi;
                } else {
                    $this->add_cmt($aid, $cmt); //添加文章评论
                }
            }
        }
    }

    //文章分类信息统计
    function show_cate() {
        $sql = 'SELECT `id`,`title`,`atc_count` FROM `categorys`';
        $result = mysql_query($sql);
        $cates = array();
        while ($rows = mysql_fetch_assoc($result)) {
            $cates[] = $rows;
        }
        $this->tpl->assign('cates', $cates);
    }

    //用户登录
    function user_login($username, $password) {
        $u_id = 0;
        // 查询当前用户名 是否存在
        $sql = "SELECT * FROM `users` WHERE `login_name`='" . $username . "' AND `password`='" . password($password) . "' LIMIT 1;";
        //$sql = 'SELECT * FROM `users` WHERE `login_name`=' . $username . ' AND `password`=' . password( $password ) . ' LIMIT 1 ';
        $result = mysql_query($sql);
        if (mysql_num_rows($result) > 0) {
            $rows = mysql_fetch_assoc($result);
            $u_id = $rows["id"];

            if (!empty($u_id)) {
                // 更新当前用户登录信息
                $sql = 'UPDATE `users` SET `last_time`=' . time() . ' , `last_ip`=\'' . getIp() . '\' ,`login_times`=`login_times`+1 WHERE `id`=' . $u_id . ' LIMIT 1 ;';
                $result = mysql_query($sql);
                // 写入SESSION
                if (!isset($_SESSION)) {
                    session_start();
                }
                foreach ($rows as $key => $value) {
                    $_SESSION[$key] = $value;
                }
            }
        }

        return $u_id;
    }

// 用户退出
    function user_logout() {
        if (!isset($_SESSION)) {
            session_start();
        }
        $_SESSION = array();
        //彻底销毁session
        session_destroy();
        show_msg('您已成功退出', '../../index.php');
    }

//显示文章页面
    function act($aid) {

        $this->act_clicks($aid);
        $sql = 'SELECT `c_id`,`u_id`,`title`,`keywords`,`description`,`content`,`pub_date`,`show_times`,`cmt_count` FROM `articles` WHERE `id`=\'' . $aid . '\'';
        $result = mysql_query($sql);
        //echo var_dump($result);
        $data = array();
        while ($rows = mysql_fetch_assoc($result)) {

            $data[] = $rows;
            $cid = $rows['c_id'];
            $uid = $rows['u_id'];
        }
        //显示所有的评论
        $this->show_cmt($aid);
        $this->tpl->assign('data', $data);
        $cate = $this->sel('title', 'categorys', $cid);
        $user = $this->sel('login_name', 'users', $uid);
        $this->tpl->assign('cate', $cate);
        $this->tpl->assign('user', $user);
        $fck_editor = fck_editor();
        $this->tpl->assign('cmt_editor', $fck_editor);
        $this->act_pn($aid); //上篇文章 下篇文章的连接
        $url = '../../index.php?sid=' . session_id() . '&amp;aid=' . $aid . '';
        $this->tpl->assign('url', $url);
        $this->tpl->display('article.tpl');
    }

    //上篇文章 下篇文章
    function act_pn($aid) {
        $sql = 'SELECT `id`,`title` FROM `articles` WHERE `id` < ' . $aid . ' ORDER BY `id` DESC LIMIT 0,1';
        $result = mysql_query($sql);
        if (mysql_num_rows($result) > 0) {
            $rows = mysql_fetch_assoc($result);
            $pre_id = $rows['id'];
            $pre_title = $rows['title'];
            $pre_url = '../../index.php?sid=' . session_id() . '&amp;aid=' . $pre_id . '';
            $this->tpl->assign('purl', $pre_url);
            $this->tpl->assign('ptitle', $pre_title);
        } else {
            $this->tpl->assign('purl', '#');
            $this->tpl->assign('ptitle', '没有了');
        }

        $sql = 'SELECT `id`,`title` FROM `articles` WHERE `id` > ' . $aid . ' ORDER BY `id` ASC LIMIT 0,1';
        $result = mysql_query($sql);
        if (mysql_num_rows($result)) {
            $rows = mysql_fetch_assoc($result);
            $nex_id = $rows['id'];
            $nex_title = $rows['title'];
            $nex_url = '../../index.php?sid=' . session_id() . '&amp;aid=' . $nex_id . '';
            $this->tpl->assign('nurl', $nex_url);
            $this->tpl->assign('ntitle', $nex_title);
        } else {
            $this->tpl->assign('nurl', '#');
            $this->tpl->assign('ntitle', '没有了');
        }
    }

//得到文章的‘点击次数’
    function act_clicks($aid) {
        global $_DBLINK;

        $click_nums = $this->sel('show_times', 'articles', $aid);
        $sql = 'UPDATE `articles` SET `show_times`=\'' . ($click_nums + 1) . '\' WHERE `id`=\'' . $aid . '\'';
        mysql_query($sql);
        if (!mysql_affected_rows($_DBLINK)) {
            echo '点击次数更新失败';
        }
    }

    //用于根据用户id、分类id等查找具体用户名及分类
    function sel($args, $tname, $id) {
        $sql = 'SELECT `' . $args . '` FROM `' . $tname . '` WHERE `id`=\'' . $id . '\'';
        $result = mysql_query($sql);
        $rows = mysql_fetch_row($result);
        $cont = $rows[0];

        return $cont;
    }

    //根据·sets·表中的key值找到相应的value值
    //找出网站标题 、网站备案号
    function sel_set($key) {
        $sql = 'SELECT `value` FROM `sets` WHERE `key`=\'' . $key . '\'';
        $result = mysql_query($sql);
        $rows = mysql_fetch_row($result);
        $cont = $rows[0];

        return $cont;
    }

    //查出当前最新文章的标题
    function sel_newtitle() {
        $sql = 'SELECT `title`,`id` FROM `articles` ORDER BY `id` DESC limit 1';
        $result = mysql_query($sql);
        $rows = mysql_fetch_row($result);
        $newTitle = $rows[0];
        $ntitle_id = $rows[1];
        $this->tpl->assign('newTitle', $newTitle);
        $this->tpl->assign('ntitle_id', $ntitle_id);
    }

    //显示所有的评论
    function show_cmt($aid) {
        $sql = 'SELECT `description`,`pub_date`,`author` FROM `comments` WHERE `atc_id`=\'' . $aid . '\'';
        $result = mysql_query($sql);
        $arr = array();
        while ($rows = mysql_fetch_assoc($result)) {
            $arr[] = $rows;
        }
        $this->tpl->assign('arr', $arr);
    }

    //添加文章评论
    function add_cmt($aid, $cmt) {
        global $_DBLINK;
        $sql = 'INSERT INTO `comments`(`atc_id`,`description`,`pub_date`,`author`) VALUES(\'' . $aid . '\',\'' . $cmt . '\',\'' . time() . '\',\'' . $_SESSION['login_name'] . '\')';
        mysql_query($sql);
        if (mysql_affected_rows($_DBLINK) > 0) {
            show_msg('发表评论成功', '../../index.php?sid=' . session_id() . '&amp;aid=' . $aid . '');
        }
        $sql = 'SELECT `atc_id`,COUNT(*) \'cmt_num\' FROM `comments` GROUP BY `atc_id`';
        $result = mysql_query($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $sql = 'UPDATE `articles` SET `cmt_count`=\'' . $rows['cmt_num'] . '\' WHERE `id`=\'' . $rows['atc_id'] . '\'';
            mysql_query($sql);
        }
    }

    //判断用户是否登录
    function isLogin() {
        if (!isset($_SESSION)) {
            session_start();
        }
        $sess_id = empty($_SESSION['id']) ? false : $_SESSION['id'];
        $this->tpl->assign('sessId', $sess_id);
        $uname = $_SESSION['login_name'];
        $this->tpl->assign('user_name', $uname);
    }

}
